// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { NotificationCenter } from "../utils/NotificationCenter";
import { readStorage } from "../utils/CommonUtils";

const {ccclass, property} = cc._decorator;

@ccclass
export default class RewardLogic extends cc.Component {
    // private bannerAd: BannerAd;
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
	@property(cc.AudioSource)
    receiveAudio:cc.AudioSource = null;

    start () {
        let openSound = Number(readStorage('open_sound'));
        if (openSound != 2) {
            this.receiveAudio.play();
        }
    }

    onDestroy() {
        if (cc.sys.WECHAT_GAME == cc.sys.platform) {
            let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
            logicCenter.bannerAd.hide();
        }
    }

    // update (dt) {}

    public setupView(iconRes: string, desc: string, scale: number = 1) {
        let icon = this.node.getChildByName('icon');
        let txt = cc.find('bg_label/label_count', this.node);
        // let im_flash = this.node.getChildByName('im_flash');
        // cc.tween(im_flash)
        //     .by(10, {angle: 360})
        //     .repeatForever()
        //     .start();
        txt.getComponent(cc.Label).string = desc;
        cc.resources.load(iconRes, cc.SpriteFrame, (err: Error, spriteFrame: cc.SpriteFrame) => {
            if (err) {
                console.log(err);
                return;
            }
            icon.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            icon.scale = scale;
        });

        if (cc.sys.WECHAT_GAME == cc.sys.platform) {
            let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
            logicCenter.bannerAd
                    .show()
                    .then(() => {
                        console.log("show ok");
                    })
                    .catch(res => {
                        console.log("show error", res);
                    });
        }
        let self = this;
        cc.tween(this.node)
            .delay(0.45)
            .call(()=>{
                self.getComponent(cc.Animation).play('gxhd-shine');
            })
            .start();
    }

    public clickClose() {
        NotificationCenter.Instance.emit("release_sub_view");
        this.node.active = false;
        this.node.destroy();
    }

}
